package com.sgb.kjwl.viewmodel.home;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.SearchDeviceOutEntity;
import com.sgb.kjwl.model.entity.SearchMachineInEntity;
import com.sgb.kjwl.model.entity.SearchMachineOutEntity;
import com.sgb.kjwl.model.entity.SearchMaterialEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.home.SearchActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.List;

import io.reactivex.functions.Consumer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SearchModel extends BaseViewModel {
    public ObservableField<String> searchValue = new ObservableField<>("");
    public ObservableField<String> numberOut = new ObservableField<>("");
    public ObservableField<String> numberIn = new ObservableField<>("");
    private HomeRepertory repertory;
    private SearchActivity activity;
    private int page = 1;
    private int size = 10;
    public int total;

    public SearchModel(SearchActivity activity, HomeRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getMaterialHotSearch() {
        /*repertory.getMaterialHotSearch().subscribe(new Consumer<List<String>>() {
            @Override
            public void accept(List<String> strings) throws Exception {
                activity.showSearchHot(strings);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                MLog.d(AppConstant.API_LOG, throwable.getMessage());
            }
        });*/

        APIManager.getInstance().getMaterialAPI().getHotWord().enqueue(new Callback<BaseEntity<List<String>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<String>>> call, Response<BaseEntity<List<String>>> response) {
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    activity.showSearchHot(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<String>>> call, Throwable t) {
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    public void getMachineHotSearch() {
       /* repertory.getMachineHotSearch().subscribe(new Consumer<List<String>>() {
            @Override
            public void accept(List<String> strings) throws Exception {
                activity.showSearchHot(strings);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                MLog.d(AppConstant.API_LOG, throwable.getMessage());
            }
        });*/

        APIManager.getInstance().getMachineAPI().getHotWord().enqueue(new Callback<BaseEntity<List<String>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<String>>> call, Response<BaseEntity<List<String>>> response) {
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    activity.showSearchHot(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<String>>> call, Throwable t) {
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    public void getDeviceHotSearch() {
        /*repertory.getDeviceHotSearch().subscribe(new Consumer<List<String>>() {
            @Override
            public void accept(List<String> strings) throws Exception {
                activity.showSearchHot(strings);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                MLog.d(AppConstant.API_LOG, throwable.getMessage());
            }
        });*/

        APIManager.getInstance().getDeviceAPI().getHotWord().enqueue(new Callback<BaseEntity<List<String>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<String>>> call, Response<BaseEntity<List<String>>> response) {
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    activity.showSearchHot(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<String>>> call, Throwable t) {
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    public void saveHistory(String value, int index) {
        repertory.saveSearchHistory(value, index)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {

                    }
                });
    }

    public void queryHistory(int index) {
        repertory.querySearchHistory(index)
                .subscribe(new Consumer<List<String>>() {
                    @Override
                    public void accept(List<String> strings) throws Exception {
                        activity.showSearchHistory(strings);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                    }
                });
    }

    public void delAllHistory(int index) {
        repertory.delAllSearchHistory(index)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        queryHistory(index);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                    }
                });
    }

    public void delHistory(String name, int index) {
        repertory.delSearchHistory(name, index)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        queryHistory(index);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                    }
                });
    }

    public void searchMaterialModel(String key) {
        page = 1;
        repertory.getMaterialSearch(repertory.getAreaCode(), repertory.getAreaCode(), key, page, size)
                .subscribe(new Consumer<ListResponse<List<SearchMaterialEntity>>>() {
                    @Override
                    public void accept(ListResponse<List<SearchMaterialEntity>> listListResponse) throws Exception {
                        total = listListResponse.getTotal();
                        if (total > 0) {
                            activity.refresh(listListResponse.getList());
                        } else {
                            activity.empty();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                        activity.empty();
                    }
                });
    }

    public void searchMaterialMoreModel(String key) {
        page = page + 1;
        repertory.getMaterialSearch(repertory.getAreaCode(), repertory.getAreaCode(), key, page, size)
                .subscribe(new Consumer<ListResponse<List<SearchMaterialEntity>>>() {
                    @Override
                    public void accept(ListResponse<List<SearchMaterialEntity>> listListResponse) throws Exception {
                        total = listListResponse.getTotal();
                        activity.loadMore(listListResponse.getList());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                        activity.loadMoreComplete();
                    }
                });
    }

    public void searchMachineOut(String key) {
        page = 1;
        repertory.getMachineSearch(page, size, repertory.getAreaCode(), key)
                .subscribe(new Consumer<BaseEntity<ListResponse<List<SearchMachineOutEntity>>>>() {
                    @Override
                    public void accept(BaseEntity<ListResponse<List<SearchMachineOutEntity>>> listResponseBaseEntity) throws Exception {
                        total = listResponseBaseEntity.getData().getTotal();
                        numberOut.set(String.valueOf(total));
                        if (total > 0) {
                            activity.refreshMachineOut(listResponseBaseEntity.getData().getList());
                        } else {
                            activity.empty();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                        activity.empty();
                    }
                });
    }

    public void searchMachineOutMore(String key) {
        page = page + 1;
        repertory.getMachineSearch(page, size, repertory.getAreaCode(), key)
                .subscribe(new Consumer<BaseEntity<ListResponse<List<SearchMachineOutEntity>>>>() {
                    @Override
                    public void accept(BaseEntity<ListResponse<List<SearchMachineOutEntity>>> listResponseBaseEntity) throws Exception {
                        total = listResponseBaseEntity.getData().getTotal();
                        activity.loadMoreMachineOut(listResponseBaseEntity.getData().getList());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                        activity.loadMoreComplete();
                    }
                });
    }

    public void searchMachineIn(String key) {
        page = 1;
        repertory.getMachineInSearch(page, size, repertory.getAreaCode(), key)
                .subscribe(new Consumer<BaseEntity<ListResponse<List<SearchMachineInEntity>>>>() {
                    @Override
                    public void accept(BaseEntity<ListResponse<List<SearchMachineInEntity>>> listResponseBaseEntity) throws Exception {
                        total = listResponseBaseEntity.getData().getTotal();
                        numberIn.set(String.valueOf(total));
                        if (total > 0) {
                            activity.refreshMachineIn(listResponseBaseEntity.getData().getList());
                        } else {
                            activity.empty();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                        activity.empty();
                    }
                });
    }

    public void searchMachineInMore(String key) {
        page = page + 1;
        repertory.getMachineInSearch(page, size, repertory.getAreaCode(), key)
                .subscribe(new Consumer<BaseEntity<ListResponse<List<SearchMachineInEntity>>>>() {
                    @Override
                    public void accept(BaseEntity<ListResponse<List<SearchMachineInEntity>>> listResponseBaseEntity) throws Exception {
                        total = listResponseBaseEntity.getData().getTotal();
                        activity.loadMoreMachineIn(listResponseBaseEntity.getData().getList());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                        activity.loadMoreComplete();
                    }
                });
    }

    public void searchDeviceOut(String key) {
        page = 1;
        repertory.getDeviceOutSearch(page, size, repertory.getAreaCode(), key)
                .subscribe(new Consumer<ListResponse<List<SearchDeviceOutEntity>>>() {
                    @Override
                    public void accept(ListResponse<List<SearchDeviceOutEntity>> listListResponse) throws Exception {
                        total = listListResponse.getTotal();
                        numberOut.set(String.valueOf(total));
                        if (total > 0) {
                            activity.refreshDeviceOut(listListResponse.getList());
                        } else {
                            activity.empty();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                        activity.empty();
                    }
                });
    }

    public void searchDeviceOutMore(String key) {
        page = page + 1;
        repertory.getDeviceOutSearch(page, size, repertory.getAreaCode(), key)
                .subscribe(new Consumer<ListResponse<List<SearchDeviceOutEntity>>>() {
                    @Override
                    public void accept(ListResponse<List<SearchDeviceOutEntity>> listListResponse) throws Exception {
                        total = listListResponse.getTotal();
                        activity.loadMoreDeviceOut(listListResponse.getList());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                        activity.loadMoreComplete();
                    }
                });
    }
}
